# -*- coding: utf-8 -*-
# 创建时间：2021/7/16 17:11
from bs4 import BeautifulSoup
import requests
import time, math, random, json
import pymysql
import re
import os

# https://github.com/JustDoPython/python-examples
# Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控

__author__ = 'LuckyHhy'


def findFilePath(path, filekeyword):
    try:
        # 获取绝对路径
        path = os.path.abspath(path)
        for x in os.listdir(path):
            # 将目录下的文件或文件夹，拼接为绝对路径
            x = os.path.join(path, x)
            # 判断是否为目录
            isdir = os.path.isdir(x)
            if isdir == False:
                filepath = os.path.abspath(x)
                # 如果是文件并且包含关键字打印出来
                if os.path.split(filepath)[1].find(filekeyword) >= 0:
                    # 打印绝对路径
                    print(os.path.abspath(x))
            else:
                # 如果是目录就进行递归调用
                subdir = os.path.abspath(x)
                findFilePath(subdir, filekeyword)
    except Exception as e:
        # 处理异常
        print("权限问题，拒绝访问！")
        raise e




def find(path, key):
    count_dirs = count_files = 0

    for root, dirs, files in os.walk(path):
        for x in files:
            if key in x:
                print(os.path.join(root, x), '文件')
                count_files += 1

        for y in dirs:
            if key in y:
                print(os.path.join(root, y), '目录')
                count_dirs += 1

    print('\n文件数：%d, 目录数：%d' % (count_files, count_dirs))


def main():
    findFilePath('D:', 'txt')
    #a = input('请输入路径：')
    #b = input('\n请输入关键字:')

    #find(a, b)



if __name__ == '__main__':
    main()
